package protocol;

import java.util.TreeMap;
import java.util.Vector;

import msgParser.Functions;
import msgParser.Parser;
import msgParser.Message;
import msgParser.Strings;

import forumSystem.Forum;
import forumSystem.ForumNotifier;
import forumSystem.ForumPolicy;
import forumSystem.ForumSystemImpl;
import forumSystem.NotificationBuilder;
import reactor.ConnectionHandler;
import tokenizer.StringMessage;


public class ForumProtocol implements AsyncServerProtocol<StringMessage> {
	
	private boolean _shouldClose = false;
	private boolean _connectionTerminated = false;
	ConnectionHandler _handler;
	private TreeMap<String, String> session;
	private ForumLogger log = ForumLogger.getInstance();
	//MessageProcessor p=new ProccessorImpl();
	//ServerAns s=new ServerAns();
	public ForumProtocol(TreeMap<String, String> connectionRecord)
	{
		this.session=connectionRecord;
		log = ForumLogger.getInstance();
	}
	
	
	
	public synchronized StringMessage processMessage(StringMessage msg, ConnectionHandler ch)
	{
		String res = null;
		System.out.println("INCOMING MSG: " + msg.getMessage());
		try
		{
			res = incomingMessage(msg.getMessage(), ch);
			_handler = ch;
		}
		catch(Exception e)
		{
			e.printStackTrace();
			return new StringMessage("MESSAGE_INVALID " + e.getMessage());
		}
		System.out.println("ANS TO CLIENT: "+ res);
		return new StringMessage(res);
	}
	
	/**
	 * This function is the main gate for incoming messages from clients.
	 * It is controlled by an exception handler from processMessage() 
	 * and therefore exceptions that are thrown will indicate an invalid message.
	 * @return String that will be sent to the client. Also indicator of a 
	 * successfully parsed message. 
	 */
	public String incomingMessage(String msg, ConnectionHandler ch) throws Exception
	{
		String msgParsed=Parser.escapeHTML(msg);
		//System.out.println(msgParsed);
		Parser p = new Parser(msgParsed);
		p.Parse();
		int flag;
		
		/*
		 * 
		 * REMOVE THIS!!!
		 * 
		 */
		if(p.testHeader("DESTROY")) System.exit(0);
		
		if(p.testHeader(Strings.INIT_FORUM))
		{
			String password=Functions.md5(p.argStr(2));
			flag=ForumSystemImpl.initForum(p.argStr(1),password);
			if(flag!=-1)
			{
				log.infoMsg(session.get("userID"), LoggerMessages.INIT_FORUM_SUCCESS);
				return Message.createMessage(Strings.INIT_OK);
			}
			else
			{
				log.warningMsg( session.get("userID"), LoggerMessages.INIT_FORUM_FAIL);
				return Message.createMessage(Strings.INIT_ERROR,ForumSystemImpl.getLastErrormsg());
			}
		}
		
		else if(p.testHeader(Strings.LOGOUT))
		{
			flag=ForumSystemImpl.logOut(this.session);
			if(flag!=-1)
			{
				log.infoMsg(session.get("userID"), LoggerMessages.USER_LOGOUT_SUCCESS);
				int userID = flag;
				UsersConections.removeConnection(userID);
				return Message.createMessage(Strings.LOGGED_OUT);
			}
			else
			{
				log.warningMsg( session.get("userID"), LoggerMessages.USER_LOGOUT_FAIL);
				return Message.createMessage(Strings.ERROR_LOG_OUT,ForumSystemImpl.getLastErrormsg());
			}
		}
		
		else if(p.testHeader(Strings.LOG_IN))
		{
			String password=Functions.md5(p.argStr(2));
			int[] arr=ForumSystemImpl.logIn(p.argStr(1),password,p.argInt(3), this.session);
			int rank;
			if(arr!=null)
			{
				log.infoMsg(session.get("userID"), LoggerMessages.USER_LOGIN_SUCCESS);
				rank=arr[1];
				UsersConections.addConnection(arr[0], ch);
				return Message.createMessage(Strings.LOGGED_IN, Integer.valueOf(arr[0]).toString(), Integer.valueOf(rank).toString());
			}
			else
			{
				log.warningMsg( session.get("userID"), LoggerMessages.USER_LOGIN_FAIL);
				return Message.createMessage(Strings.ERROR_LOG_IN,ForumSystemImpl.getLastErrormsg());
			}
		}
		
		else if(p.testHeader(Strings.REGISTER))
		{
			String password=Functions.md5(p.argStr(2));
			flag=ForumSystemImpl.register(p.argStr(1),password,p.argStr(3),p.argInt(4), this.session);
			if(flag!=-1)
			{
				if (flag == -2)
					return Message.createMessage(Strings.ACTIVATION_NEEDED, Integer.valueOf(flag).toString());
				log.infoMsg(session.get("userID"), LoggerMessages.GUEST_REGISTER_SUCCESS);
				return Message.createMessage(Strings.REGISTERED, Integer.valueOf(flag).toString());
			}
			else
			{
				log.warningMsg( session.get("userID"), LoggerMessages.GUEST_REGISTER_FAIL);
				return Message.createMessage(Strings.ERROR_REGISTER,ForumSystemImpl.getLastErrormsg());
			}
		}
		
		else if(p.testHeader(Strings.ACTIVATION_REQUEST))
		{
			flag=ForumSystemImpl.activationReq(p.argStr(1), p.argStr(2),this.session);
			if(flag!=-1)
			{
				//log.infoMsg(session.get("userID"), LoggerMessages.GUEST_REGISTER_SUCCESS);
				return Message.createMessage(Strings.REGISTERED, Integer.valueOf(flag).toString());
			}
			else
			{
				// log.warningMsg( session.get("userID"), LoggerMessages.GUEST_REGISTER_FAIL);
				return Message.createMessage(Strings.ERROR_REGISTER,ForumSystemImpl.getLastErrormsg());
			}
		}
		
		else if(p.testHeader(Strings.ADD_FORUM))
		{
			int policyID=ForumSystemImpl.createPolicy(p.argInt(3),p.argInt(4),p.argInt(5),p.argInt(6),p.argInt(7),p.argInt(8),p.argInt(9), p.argInt(10) ,this.session);
			flag=ForumSystemImpl.createForum(p.argStr(1),p.argInt(2),policyID, this.session);
			
			if(flag!=-1)
			{
				ForumNotifier fn = ForumNotifier.InitNotifier(flag, p.argInt(5));
				ForumSystemImpl.activateNotifier(fn);
				log.infoMsg(session.get("userID"), LoggerMessages.ADD_FORUM_SUCCESS);
				return Message.createMessage(Strings.FORUM_ADDED, Integer.valueOf(flag).toString());
			}
			else
			{
				log.warningMsg( session.get("userID"), LoggerMessages.ADD_FORUM_FAIL);
				return Message.createMessage(Strings.ERROR_FORUM_ADD,ForumSystemImpl.getLastErrormsg());
			}
		}
		
		else if(p.testHeader(Strings.ADD_SUBFORUM))
		{
			flag=ForumSystemImpl.createSubForum(p.argStr(1),p.argInt(2),p.argInt(3), this.session);
			if(flag!=-1)
			{
				log.infoMsg(session.get("userID"), LoggerMessages.ADD_SUBFORUM_SUCCESS);
				return Message.createMessage(Strings.SUBFORUM_ADDED, Integer.valueOf(flag).toString());
			}
			else
			{
				log.warningMsg( session.get("userID"), LoggerMessages.ADD_SUBFORUM_FAIL);
				return Message.createMessage(Strings.ERROR_ADD_SUBFORUM,ForumSystemImpl.getLastErrormsg());
			}
		}
		
		else if(p.testHeader(Strings.ADD_TOPIC))
		{
			flag=ForumSystemImpl.createTopic(p.argStr(1),p.argStr(2),p.argInt(3), this.session);
			if(flag!=-1)
			{
				log.infoMsg(session.get("userID"), LoggerMessages.ADD_TOPIC_SUCCESS);
				return Message.createMessage(Strings.TOPIC_ADDED, Integer.valueOf(flag).toString());
			}
			else
			{
				log.warningMsg( session.get("userID"), LoggerMessages.ADD_TOPIC_FAIL);
				return Message.createMessage(Strings.ERROR_ADD_TOPIC,ForumSystemImpl.getLastErrormsg());
			}
		}
		
		else if(p.testHeader(Strings.ADD_REPLY))
		{
			flag=ForumSystemImpl.createReply(p.argStr(1),p.argInt(2), this.session);
			if(flag!=-1)
			{
				NotificationBuilder.buildNotification(flag, "addReply");
				log.infoMsg(session.get("userID"), LoggerMessages.FORUM_CHANGED);
				return Message.createMessage(Strings.REPLY_ADDED, Integer.valueOf(flag).toString());
			}
			else
			{
				return Message.createMessage(Strings.ERROR_ADD_REPLY,ForumSystemImpl.getLastErrormsg());
			}
		}
		
		else if(p.testHeader(Strings.SHOW_FORUMS))
		{
			Vector<String> v =ForumSystemImpl.getAllForumsWithIDs();
			if(v!=null)
			{
				return Message.createMessage(Strings.ANS_SHOW_FORUMS, v);
			}
			else
			{
				return Message.createMessage(Strings.ERROR_SHOW_FORUMS,ForumSystemImpl.getLastErrormsg());
			}
		}
		
		else if(p.testHeader(Strings.SHOW_SUBFORUMS))
		{
			Vector<String> v =ForumSystemImpl.getAllSubForums(p.argInt(1));
			if(v!=null)
			{
				return Message.createMessage(Strings.ANS_SHOW_SUBFORUMS, v);
			}
			else
			{
				return Message.createMessage(Strings.ERROR_SHOW_SUBFORUMS,ForumSystemImpl.getLastErrormsg());
			}
		}
		
		else if(p.testHeader(Strings.SHOW_TOPICS))
		{
			Vector<String> v =ForumSystemImpl.getAllTopicsInSubForum(p.argInt(1));
			if(v!=null)
			{
				return Message.createMessage(Strings.ANS_SHOW_TOPICS, v);
			}
			else
			{
				return Message.createMessage(Strings.ERROR_SHOW_TOPICS,ForumSystemImpl.getLastErrormsg());
			}
		}
		
		else if(p.testHeader(Strings.SHOW_MESSAGES))
		{
			Vector<String> v =ForumSystemImpl.getAllMessages(p.argInt(1));
			if(v!=null)
			{
				return Message.createMessage(Strings.ANS_SHOW_MESSAGES, v);
			}
			else
			{
				return Message.createMessage(Strings.ERROR_SHOW_MESSAGES,ForumSystemImpl.getLastErrormsg());
			}
		}
		
		else if(p.testHeader(Strings.SHOW_SYSTEM_USERS))
		{
			Vector<String> v =ForumSystemImpl.getAllUsers(this.session);
			if(v!=null)
			{
				return Message.createMessage(Strings.ANS_SHOW_SYSTEM_USERS, v);
			}
			else
			{
				return Message.createMessage(Strings.ERROR_SHOW_SYSTEM_USERS,ForumSystemImpl.getLastErrormsg());
			}
		}
		
		else if(p.testHeader(Strings.REMOVE_TOPIC))
		{
			
			flag=ForumSystemImpl.removeTopic(p.argInt(1), this.session);
			if(flag!=-1)
			{
				log.infoMsg(session.get("userID"), LoggerMessages.REMOVE_TOPIC_SUCCESS);
				return Message.createMessage(Strings.TOPIC_REMOVED);
			}
			else
			{
				log.warningMsg( session.get("userID"), LoggerMessages.REMOVE_TOPIC_FAIL);
				return Message.createMessage(Strings.ERROR_REMOVE_TOPIC,ForumSystemImpl.getLastErrormsg());
			}
		}
		
		else if(p.testHeader(Strings.ADD_MODERATOR))
		{
			flag=ForumSystemImpl.addModerator(p.argInt(1), p.argInt(2), this.session);
			if(flag!=-1)
			{
				log.infoMsg(session.get("userID"), LoggerMessages.MODERATOR_ADDED_SUCCESS);
				return Message.createMessage(Strings.MODERATOR_ADDED);
			}
			else
			{
				log.warningMsg( session.get("userID"), LoggerMessages.MODERATOR_ADDED_FAIL);
				return Message.createMessage(Strings.ERROR_ADD_MODERATOR,ForumSystemImpl.getLastErrormsg());
			}
		}
		
		else if(p.testHeader(Strings.ADD_ADMIN))
		{
			flag=ForumSystemImpl.addAdmin(p.argInt(1), p.argInt(2), this.session);
			if(flag!=-1)
			{
				log.infoMsg(session.get("userID"), LoggerMessages.ADD_MODERATOR_SUCCESS);
				return Message.createMessage(Strings.ADMIN_ADDED);
			}
			else
			{
				log.warningMsg( session.get("userID"), LoggerMessages.ADD_MODERATOR_FAIL);
				return Message.createMessage(Strings.ERROR_ADD_ADMIN,ForumSystemImpl.getLastErrormsg());
			}
		}
		
		else if(p.testHeader(Strings.REMOVE_SUBFORUM))
		{
			flag=ForumSystemImpl.removeSubForum(p.argInt(1), this.session);
			if(flag!=-1)
			{
				log.infoMsg(session.get("userID"), LoggerMessages.REMOVE_SUBFORUM_SUCCESS);
				return Message.createMessage(Strings.SUBFORUM_REMOVED);
			}
			else
			{
				log.warningMsg( session.get("userID"), LoggerMessages.REMOVE_SUBFORUM_FAIL);
				return Message.createMessage(Strings.ERROR_REMOVE_SUBFORUM,ForumSystemImpl.getLastErrormsg());
			}
		}
		
		else if(p.testHeader(Strings.REMOVE_MODERATOR))
		{
			flag=ForumSystemImpl.removeModerator(p.argInt(1), p.argInt(2), this.session);
			if(flag!=-1)
			{
				return Message.createMessage(Strings.MODERATOR_REMOVED);
			}
			else
			{
				return Message.createMessage(Strings.ERROR_REMOVE_MODERATOR,ForumSystemImpl.getLastErrormsg());
			}
		}
		
		else if(p.testHeader(Strings.GET_SUBFORUM_SIZE))
		{
			flag=ForumSystemImpl.getSubForumSize(p.argInt(1), this.session);
			if(flag!=-1)
			{
				return Message.createMessage(Strings.ANS_GET_SUBFORUM_SIZE, Integer.valueOf(flag).toString());
			}
			else
			{
				return Message.createMessage(Strings.ERROR_GET_SUBFORUM_SIZE,ForumSystemImpl.getLastErrormsg());
			}
		}
		
		else if(p.testHeader(Strings.GET_USER_MSGCOUNT))
		{
			flag=ForumSystemImpl.getUserMsgCount(p.argInt(1), this.session);
			if(flag!=-1)
			{
				return Message.createMessage(Strings.ANS_GET_USER_MSGCOUNT, Integer.valueOf(flag).toString());
			}
			else
			{
				return Message.createMessage(Strings.ERROR_GET_USER_MSGCOUNT,ForumSystemImpl.getLastErrormsg());
			}
		}
		
		else if(p.testHeader(Strings.GET_FORUM_COUNT))
		{
			flag=ForumSystemImpl.getForumCount();
			if(flag!=-1)
			{
				return Message.createMessage(Strings.ANS_GET_FORUM_COUNT, Integer.valueOf(flag).toString());
			}
			else
			{
				return Message.createMessage(Strings.ERROR_GET_FORUM_COUNT,ForumSystemImpl.getLastErrormsg());
			}
		}
		
		else if(p.testHeader(Strings.GET_COMMON_USERS))
		{
			Vector<String> v=ForumSystemImpl.getCommonUsers(this.session);
			if(v!=null)
			{
				return Message.createMessage(Strings.ANS_GET_COMMON_USERS, v);
			}
			else
			{
				return Message.createMessage(Strings.ERROR_GET_COMMON_USERS,ForumSystemImpl.getLastErrormsg());
			}
		}
		
		else if(p.testHeader(Strings.GET_AUTHORS_OF_REPLIES_TO_USER))
		{
			Vector<String> v =ForumSystemImpl.getAllUsersThatReplyToID(p.argInt(1),this.session);
			if(v!=null)
			{
				return Message.createMessage(Strings.ANS_GET_AUTHORS_OF_REPLIES_TO_USER, v);
			}
			else
			{
				return Message.createMessage(Strings.ERROR_GET_AUTHORS_OF_REPLIES_TO_USER,ForumSystemImpl.getLastErrormsg());
			}
		}
		
		else if(p.testHeader(Strings.SHOW_FORUM_USERS))
		{
			Vector<String> v =ForumSystemImpl.getForumUsers(p.argInt(1),this.session);
				
			if(v!=null)
			{
				
				return Message.createMessage(Strings.ANS_SHOW_FORUM_USERS, v);
			}
			else
			{
				return Message.createMessage(Strings.ERROR_SHOW_FORUM_USERS,ForumSystemImpl.getLastErrormsg());
			}
		}
		
		else if(p.testHeader(Strings.EDIT_REPLY))
		{
			flag=ForumSystemImpl.editReply(p.argStr(1), p.argInt(2), this.session);
			if(flag!=-1)
			{
				NotificationBuilder.buildNotification(p.argInt(2),"edit_reply");
				log.infoMsg(session.get("userID"), LoggerMessages.EDIT_REPLY_SUCCESS);
				return Message.createMessage(Strings.REPLY_EDITED);
			}
			else
			{
				log.warningMsg( session.get("userID"), LoggerMessages.EDIT_REPLY_FAIL);
				return Message.createMessage(Strings.ERROR_EDIT_REPLY,ForumSystemImpl.getLastErrormsg());
			}
		}
		
		else if(p.testHeader(Strings.EDIT_POST))
		{
			flag=ForumSystemImpl.editPost(p.argStr(1), p.argInt(2), this.session);
			if(flag!=-1)
			{
				
				int postId = p.argInt(2);
				NotificationBuilder.buildNotification(postId, "edit_post");
				log.infoMsg(session.get("userID"), LoggerMessages.FORUM_CHANGED);
				return Message.createMessage(Strings.POST_EDITED);
			}
			else
			{
				return Message.createMessage(Strings.ERROR_EDIT_POST,ForumSystemImpl.getLastErrormsg());
			}
		}
		
		else if(p.testHeader(Strings.REMOVE_FORUM))
		{
			flag=ForumSystemImpl.removeForum(p.argInt(1),this.session);
			if(flag!=-1)
			{
				return Message.createMessage(Strings.FORUM_REMOVED);
			}
			else
			{
				return Message.createMessage(Strings.ERROR_REMOVE_FORUM,ForumSystemImpl.getLastErrormsg());
			}
		}
		else if(p.testHeader(Strings.REMOVE_REPLY))
		{
			flag=ForumSystemImpl.removeReply(p.argInt(1),this.session);
			if(flag!=-1)
			{
				log.infoMsg(session.get("userID"), LoggerMessages.REMOVE_REPLY_SUCCESS);
				return Message.createMessage(Strings.REPLY_REMOVED);
			}
			else
			{
				log.warningMsg( session.get("userID"), LoggerMessages.REMOVE_REPLY_FAIL);
				return Message.createMessage(Strings.ERROR_REMOVE_REPLY,ForumSystemImpl.getLastErrormsg());
			}
		}
		
		else if(p.testHeader("ADMIN_LOGIN"))
		{
			String password=Functions.md5(p.argStr(2));
			flag=ForumSystemImpl.adminLogin(p.argStr(1), password,this.session);
			if(flag!=-1)
			{
				return Message.createMessage(Strings.LOGGED_IN, Integer.valueOf(flag).toString(), "4");
			}
			else
			{
				return Message.createMessage(Strings.ERROR_LOG_IN,ForumSystemImpl.getLastErrormsg());
			}
		}
		
		else if(p.testHeader(Strings.GET_SUBFORUM_MODERATORS))
		{
			Vector<String> v =ForumSystemImpl.get_subforum_moderators(p.argInt(1),this.session);
			if(v!=null)
			{
				return Message.createMessage(Strings.ANS_GET_SUBFORUM_MODERATORS, v);
			}
			else
			{
				return Message.createMessage(Strings.ERROR_GET_SUBFORUM_MODERATORS, ForumSystemImpl.getLastErrormsg());
			}
		}

		return Message.createMessage(Strings.MESSAGE_INVALID);
	}
	
	/*
	
	
	public StringMessage processMessage______OLD(StringMessage msg) {
		String msgString=msg.getMessage();
		if(p.isInitMessage(msgString))
		{
			Reactor.db.initialize(p.getSuperAdminUsername(msgString), 
					p.getSuperAdminPass(msgString));
			return new StringMessage(s.AnsToInit());
		}
		if(p.isAddForumMessage(msgString))
		{
			String superAdminid=Reactor.db.getUserByUserName(Reactor.db.getAdminUserName()).getId()+"";
			if(this.session.get("userID")!=null && this.session.get("userID").equals(superAdminid))
			{
				int forumID=Reactor.db.addForum(p.getAddForumTitle(msgString), "no need for description");
				if(forumID!=-1)
				{
					return new StringMessage(s.AnsToAddForumTrue(forumID));
				}
				else
				{
					return new StringMessage(s.AnsToAddForumFalse());
				}
			}
			else
			{
				return new StringMessage("forumAddError ; not superAdmin");
			}

		}
		if(p.isShowAllForumsRequest(msgString))
		{
			Vector<Forum> forumlist=Reactor.db.getForumlist();
			if(forumlist!=null)
			{
				return new StringMessage(s.ShowAllForumsAns(forumlist));
			}
			else
			{
				return new StringMessage(s.ShowAllForumsFalse());
			}
		}
		if(p.isShowAllUsersRequest(msgString))
		{
			String superAdminid=Reactor.db.getUserByUserName(Reactor.db.getAdminUserName()).getId()+"";
			if(this.session.get("userID")!=null && this.session.get("userID").equals(superAdminid))
			{
				Vector<User> userList=Reactor.db.getUsers();
				if(userList!=null)
				{
					return new StringMessage(s.ShowAllUsersAns(userList));
				}
				else
				{
					return new StringMessage(s.ShowAllUsersFalse());
				}
			}
			else
			{
				return new StringMessage(Constants.showUsersFalse+" "+Constants.seperator+" "+"not a super admin");
			}
		}
		if(p.isLoginRequest(msgString))
		{
			if(this.session.get("isOnline")!=null && this.session.get("isOnline").equals("true"))
			{
				return new StringMessage("LoginError ; already loggedIn");
			}
			if(Reactor.db.isUserExist(p.getLoginUserName(msgString), p.getLoginPass(msgString)))
			{
				int userID=Reactor.db.getUserByUserName(p.getLoginUserName(msgString)).getId();
				this.session.put("isOnline", "true");
				this.session.put("userID", String.valueOf(Reactor.db.getUserByUserName(p.getLoginUserName(msgString)).getId()));
				return new StringMessage(s.ansToLoginTrue(userID));
			}
			else
			{
				return new StringMessage(s.ansToLoginFalse());
			}
		}
		if(p.isRegisterRequest(msgString))
		{
			if(this.session.get("isOnline")!=null && this.session.get("isOnline").equals("true"))
			{
				return new StringMessage("RegisterError ; logged in users cant register");
			}
			int userID=Reactor.db.addUser(p.getRegisterUserName(msgString), p.getRegisterPass(msgString));
			if(userID!=-1)
			{
				return new StringMessage(s.ansToRegisterTrue(userID));
			}
			else
			{
				return new StringMessage(s.ansToRegisterFalse());
			}
		}
		if(p.isCreateSubForumRequest(msgString))
		{
			int forumid=p.getSubForumParent(msgString);
			if(this.session.get("userID")!=null && 
					(Integer.parseInt(this.session.get("userID"))==Reactor.db.getForumByID(forumid).getManagerID() 
					||
					Integer.parseInt(this.session.get("userID"))==Reactor.db.getUserByUserName(Reactor.db.getAdminUserName()).getId()))
			{
				int subForumID=Reactor.db.addSubForum(p.getSubForumName(msgString),
						"description not needed for now", 
						p.getSubForumParent(msgString));
				if(subForumID!=-1)
				{
					return new StringMessage(s.ansToSubForumAddTrue(subForumID));
				}
				else
				{
					return new StringMessage(s.ansToSubForumAddFalse());
				}
			}
			else
			{
				return new StringMessage(Constants.newsubforumFalse+" "+Constants.seperator+" "+"not forum manager or super admin");
			}
		}
		if(p.isShowForum(msgString))
		{
			int forumID=p.getShowForumID(msgString);
			Vector<SubForum> posts=Reactor.db.getSubForumslist(forumID);
			if(posts!=null)
			{
				return new StringMessage(s.ansToGetPostsTrue(posts));
			}
			else
			{
				return new StringMessage(s.ansToGetPostsFalse());
			}
		}
		if(p.isShowPost(msgString))
		{
			int postID=p.getpostID(msgString);
			Post p=Reactor.db.getPostByID(postID);
			if(p!=null)
			{
				return new StringMessage(s.ansToShowPostsTrue(p));
			}
			else
			{
				return new StringMessage(s.ansToShowPostsFalse());
			}
		}
		if(p.isShowSubForum(msgString))
		{
			int forumID=p.getShowSubForumID(msgString);
			Vector<Post> posts=Reactor.db.getSubForumPosts(forumID);
			if(posts!=null)
			{
				return new StringMessage(s.ansToGetSubForumTrue(posts));
			}
			else
			{
				return new StringMessage(s.ansToGetSubForumFalse());
			}
		}
		if(p.isNewMessageRequest(msgString))
		{
			if(this.session.get("userID")==null)
			{
				return new StringMessage("newMessageError ; only loggedin members can add message");
			}
			else
			{
				int postID=Reactor.db.addPost(Integer.parseInt(this.session.get("userID")), 
						p.newMessageSubForumID(msgString), 
						p.newMessageTitle(msgString), 
						p.newMessageDesc(msgString));
				if(postID!=-1)
				{
					return new StringMessage(s.ansToNewMessageTrue(postID));
				}
				else
				{
					return new StringMessage(s.ansToNewMessageFalse());
				}
			}
		}
		if(p.isNewReaplyRequest(msgString))
		{
			if(this.session.get("userID")==null)
			{
				return new StringMessage("newReplyError ; only loggedin members can add message");
			}
			else
			{
				int replyID=Reactor.db.addReply(Integer.parseInt(this.session.get("userID")), 
						p.newReaplyPostID(msgString), 
						p.newReaplyContent(msgString));
				if(replyID!=-1)
				{
					return new StringMessage(s.ansToNewReplyTrue(replyID));
				}
				else
				{
					return new StringMessage(s.ansToNewReplyFalse());
				}
			}
		}
		if(p.isLogout(msgString))
		{
			if(this.session.get("userID")==null)
			{
				return new StringMessage("logoutError ; only loggedin members can logout");
			}
			else
			{
				this.session.remove("isOnline");
				this.session.remove("userID");
				return new StringMessage("loggedOut");
			}
		}
		return new StringMessage("not leagal command");
	}
	
	*/
	
	@Override
	public boolean isEnd(StringMessage msg) {
		return msg.getMessage().equals("close program");
	}

	@Override
	public boolean shouldClose() {
		// TODO Auto-generated method stub
		return this._shouldClose;
	}

	@Override
	public void connectionTerminated() {
		// TODO Auto-generated method stub
		
	}
	
	
}
